$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/employee/list',
        datatype: "json",
        colModel: [			
			{ label: '员工序号', name: 'employeeno', index: 'employeeNo', width: 100, key: true },
			{ label: '员工角色', name: 'employeetype', index: 'employeeType', width: 100 }, 			
			{ label: '转正日期', name: 'confirmdate', index: 'confirmDate', width: 80 ,formatter:"date", formatoptions: {newformat:'Y年m月d日'}}, 			
			{ label: '员工姓名', name: 'name', index: 'Name', width: 100 }, 			
			{ label: '员工性别', name: 'gender', index: 'gender', width: 100 }, 			
			{ label: '员工生日', name: 'birthday', index: 'birthday', width: 100 ,formatter:"date", formatoptions: {newformat:'Y年m月d日'} }, 			
			{ label: '员工证件类型', name: 'idtype', index: 'IdType', width: 100 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/cardtype/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.cardtype.cardname;
						}
					}
				});
				return result;
			}}, 			
			{ label: '员工证件号码', name: 'idno', index: 'IDNo', width: 100 }, 			
			{ label: '合同起期', name: 'startdate', index: 'Startdate', width: 100 ,formatter:"date", formatoptions: {newformat:'Y年m月d日'}}, 			
			{ label: '合同止期', name: 'enddate', index: 'EndDate', width: 100 ,formatter:"date", formatoptions: {newformat:'Y年m月d日'}}, 			
			{ label: '入职日期', name: 'entrydate', index: 'EntryDate', width: 100 ,formatter:"date", formatoptions: {newformat:'Y年m月d日'}}, 			
			{ label: '校区', name: 'schoolno', index: 'schoolNo', width: 50 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/school/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.school.schoolname;
						}
					}
				});
				return result;
			}}, 			
			{ label: '部门', name: 'deptno', index: 'deptNo', width: 50 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/department/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.department.name;
						}
					}
				});
				return result;
			}}, 			
			{ label: '职务', name: 'job', index: 'job', width: 80 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/jobcode/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.jobcode.dutyname;
						}
					}
				});
				return result;
			}}, 			
			{ label: '职级', name: 'jobgrade', index: 'jobGrade', width: 80 ,formatter: function(value, options, row){
				var result = "";
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/jobgradecode/info/"+value,
	                contentType: "application/json",
				    async : false,
				    success: function(r){
				    	if(r.code === 0){
				    		result = r.jobgradecode.jobgradename;
						}
					}
				});
				return result;
			}}, 			
			{ label: '工资基准', name: 'salarygrade', index: 'salaryGrade', width: 80 }, 			
			{ label: '联系电话', name: 'phone', index: 'phone', width: 80 }, 			
			{ label: '微信', name: 'webchat', index: 'webchat', width: 80 }, 			
			{ label: '紧急联系人', name: 'contacter', index: 'contacter', width: 80 }, 			
			{ label: '紧急联系人电话', name: 'conphone', index: 'conphone', width: 80 }, 			
			{ label: '备用1', name: 'standbyflag1', index: 'StandByFlag1', width: 80 }, 			
			{ label: '备用2', name: 'standbyflag2', index: 'StandByFlag2', width: 80 }, 			
			{ label: '备用3', name: 'standbyflag3', index: 'StandByFlag3', width: 80 }		
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		employee: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.employee = {};
			this.initSelect();
		},
		update: function (event) {
			var employeeno = getSelectedRow();
			this.initSelect();
			if(employeeno == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(employeeno)
		},
		saveOrUpdate: function (event) {
			var url = vm.employee.employeeno == null ? "generator/employee/save" : "generator/employee/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.employee),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var employeenos = getSelectedRows();
			if(employeenos == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "generator/employee/delete",
                    contentType: "application/json",
				    data: JSON.stringify(employeenos),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(employeeno){
			$.get(baseURL + "generator/employee/info/"+employeeno, function(r){
                vm.employee = r.employee;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		initSelect: function () {//下拉列表
			$.ajax({
				type: "POST",
			    url: baseURL + "generator/employee/findAllinit",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						//学校
						var school = r.page.school;
						var $school = $("#schoolno");
						$school.empty();
						$school.append("<option selected='selected'>请选择</option>");
						if("undefined" != school){
							for(var i = 0; i < school.length; i++){
								$school.append("<option value='"+school[i].schoolno+"'>"+school[i].schoolname+"</option>");
							}
						}
						//部门
						var department = r.page.department;
						var $deptno = $("#deptno");
						$deptno.empty();
						$deptno.append("<option selected='selected'>请选择</option>");
						if("undefined" != department){
							for(var i = 0; i < department.length; i++){
								$deptno.append("<option value='"+department[i].departmentno+"'>"+department[i].name+"</option>");
							}
						}
						//职务
						var jobcode = r.page.jobcode;
						var $job = $("#job");
						$job.empty();
						$job.append("<option selected='selected'>请选择</option>");
						if("undefined" != jobcode){
							for(var i = 0; i < jobcode.length; i++){
								$job.append("<option value='"+jobcode[i].code+"'>"+jobcode[i].dutyname+"</option>");
							}
						}
						//证件类型
						var cardtype = r.page.cardtype;
						var $idtype = $("#idtype");
						$idtype.empty();
						$idtype.append("<option selected='selected'>请选择</option>");
						if("undefined" != cardtype){
							for(var i = 0; i < cardtype.length; i++){
								$idtype.append("<option value='"+cardtype[i].code+"'>"+cardtype[i].cardname+"</option>");
							}
						}
						//职级
						var jobgradecode = r.page.jobgradecode;
						var $jobgrade = $("#jobgrade");
						$jobgrade.empty();
						$jobgrade.append("<option selected='selected'>请选择</option>");
						if("undefined" != jobgradecode){
							for(var i = 0; i < jobgradecode.length; i++){
								$jobgrade.append("<option value='"+jobgradecode[i].code+"'>"+jobgradecode[i].jobgradename+"</option>");
							}
						}
					}else{
						alert(r.msg);
					}
				}
			});
		}
	}
});